package com.cy.mall.web;

import javax.servlet.http.HttpServletRequest;

import com.cy.mall.model.enums.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;

import com.cy.mall.comm.constant.Constants;
import com.cy.mall.comm.exception.MsgException;
import com.cy.mall.comm.utils.CommonUtils;
import com.cy.mall.model.Result;
import com.cy.mall.model.dto.customer.CustomerLoginDTO;
import com.cy.mall.model.enums.SmsType;

/**
 * 控制器 BaseController
 *
 * @author: tanyilin
 * @create: 2018年10月26日 下午2:37:19
 */
public class BaseController {

	@Autowired
    protected RedisTemplate redisTemplate;
    @Autowired
    protected HttpServletRequest request;
	
    /**
     *  异常
     * 
     * @param expression
     * @param message
     */
	protected void state(boolean expression, String message) {
		if(!expression) {
			throw new MsgException(message);
		}
	}
	
	/**
	 * 校验错误信息
	 * 
	 * @param bindingResult
	 */
	protected void checkError(BindingResult bindingResult) {
		if(!bindingResult.hasErrors()) {
			return;
		}
		String message = bindingResult.getAllErrors().get(0).getDefaultMessage();
		throw new MsgException(message); 
	} 
	
	protected Result ok() {
		return Result.success();
	}
	
	protected Result ok(String message) {
		return Result.success(message);
	}
	
	protected Result ok(String message, Object value) {
		return Result.success(message, value);
	}

	protected Result ok(String message, ResultCode code) {
		return Result.success(message, code);
	}

	protected Result ok(String message, ResultCode code, Object value) {
		return Result.success(message, code, value);
	}
	
	protected Result fail(String message) {
		return Result.fail(message);
	}
	
	/**
     * 检验短信验证码
     * @param mobile
     * @param code
     * @param type
     */
    protected void checkSmsCode(String mobile, String code, SmsType type) {
        String key = CommonUtils.smsCodeKey(mobile, type);
        Object obj = redisTemplate.opsForValue().get(key);
        if (obj == null) {
            throw new MsgException("验证码已失效。");
        }

        if (!String.valueOf(obj).equals(code)) {
            throw new MsgException("验证码错误。");
        }
        
        //验证码验证通过，删除验证码缓存
        redisTemplate.delete(key);
    }
	
    /**
     * 得到已登录的客户信息
     * 
     * @return
     */
    protected CustomerLoginDTO getLoginCustomer() {
    	String token = request.getHeader("token");
    	if (CommonUtils.isEmpty(token)) {
            throw new MsgException("请求没有发现token");
        }
    	String key = Constants.CUSTOMER_LOGIN_KEY.concat(token);
    	CustomerLoginDTO customerLoginDTO = (CustomerLoginDTO) redisTemplate.opsForValue().get(key);
    	return customerLoginDTO;
    }
    
    /**
     * 覆盖客户登录信息
     * 
     */
    protected void expireCustomerLoginDTO(String token, CustomerLoginDTO customerLoginDTO) {
    	String key = Constants.CUSTOMER_LOGIN_KEY.concat(token);
    	//把客户登录信息存到redis
		redisTemplate.opsForValue().set(key, customerLoginDTO, Constants.LOGIN_TIME, Constants.LOGIN_TIME_UNIT);
    }
    
}
